<?php
namespace Common\Behaviors;

use Think\Behavior;
use Common\Logic\Common\ConfigLogic;

class GlobalBehavior extends Behavior
{
    public function run(&$params)
    {
        $this->defineVars();
        $this->checkInitJavascript();
        $this->globalConfig();
    }

    //检测IE版本
    public function checkIEVersion()
    {
        $ie = IEVersion();
        if($ie < 1){ return; }
        if(CONTROLLER_NAME != 'Browser'){
            if($ie < 9){
                redirect(U('admin/browser/index'));
            }
        }
    }

    //定义一些常量
    public function defineVars()
    {
        define('MODULE_DOMAIN', strtolower(HTTP_HTTPS.MODULE_NAME.'.'.DOMAIN));
    }

    //生成 JS 全局变量
    public function checkInitJavascript()
    {
        $init_file = $_SERVER['DOCUMENT_ROOT'].'/js/common/init.js';
        if(is_file($init_file)){ return; }
        $js = 'var G_HTTP_HTTPS = "'.HTTP_HTTPS.'";
var G_DOMAIN = "'.DOMAIN.'";';
        file_put_contents($init_file, $js);
    }

    //设置全局config
    public function globalConfig()
    {
        $fn = 'global_cfg';
        $config = F($fn);
        if(empty($config)){
            $con = new ConfigLogic();
            /* 读取站点配置 */
            $config = $con->getGlobalConfig();
            F($fn, $config);
        }
        C($config);//添加配置
    }
}